package hotLeetcode;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class H739Leetcode {
    public static int[] dailyTemperatures1(int[] temperatures) {

        int[] res = new int[temperatures.length];
        for (int i = 0; i < temperatures.length; i++) {
            for (int j = i + 1; j < temperatures.length; j++) {
                if (temperatures[j] > temperatures[i]) {
                    res[i] = j - i;
                    break;
                }



            }

        }
        return res;

    }


    public static int[] dailyTemperatures(int[] temperatures) {
        int n = temperatures.length;
        int[] ans = new int[n];
        Deque<Integer> st = new ArrayDeque<>();
        for (int i = n - 1; i >= 0; i--) {
            int t = temperatures[i];
            while (!st.isEmpty() && t >= temperatures[st.peek()]) {
                st.pop();

            }

            if (!st.isEmpty()) {
                ans[i] = st.peek() - i;
            }


            st.push(i);

        }

        return ans;

    }

    public static void main(String[] args) {
        int[] t = new int[]{73, 74, 75, 71, 69, 72, 76, 73};
        System.out.println(Arrays.toString(H739Leetcode.dailyTemperatures(t)));
    }
}
